package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/liangchao/blogo/src/common/Response"
	"github.com/liangchao/blogo/src/common/dao"
	"github.com/liangchao/blogo/src/common/models"
	"log"
	"net/http"
	"strconv"
)

var (
	blogDao       = new(dao.BlogDao)
	cateDao       = new(dao.CategoryDao)
	tagDao       = new(dao.TagDao)
	hotBlogMax    = 3  // 显示主页 limit 最热博客
	recentBlogMax = 10 // 显示主页
)

// GetHomePage 初始化主页
func GetHomePage(c *gin.Context) {
	// 查询最热的三个博客
	//hotBlogs, err := blogDao.GetHotBlog(hotBlogMax)
	//if err != nil {
	//	Response.Err(c, 200, 500, err.Error(), "")
	//	return
	//}

	// 查询最近的博客
	recentBlogs, err := blogDao.GetRecentBlogList(recentBlogMax)
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "")
	}

	//// 查询所有分类
	//allCategory, err := cateDao.GetCategoryAllList()
	//if err != nil {
	//	Response.Err(c, 200, 500, err.Error(), "")
	//}

	c.HTML(http.StatusOK, "template/index.html", gin.H{
		"Title":       "主页",
		//"HotBlogs":    hotBlogs,
		"RecentBlogs": recentBlogs,
		//"AllCategory": allCategory,
	})
}

// QueryBlogListByKeywordsPage 查询博客列表根据关键字
func QueryBlogListByKeywordsPage(c *gin.Context) {
	page, _ := strconv.Atoi(c.Param("page"))
	limit, _ := strconv.Atoi(c.Param("limit"))
	keywords := c.DefaultQuery("keywords", "")
	method, _ := strconv.Atoi(c.DefaultQuery("method", "1"))

	blogs, total, err := blogDao.QueryBlogListByKeyworks(keywords, method, (page-1)*limit, limit)
	if err != nil {
		log.Println(err)
		return
	}

	// 查询所有分类
	allCategory, err := cateDao.GetCategoryAllList()
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "")
	}

	// 插叙所有标签
	allTags, err := tagDao.GetAllTagName()
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "")
	}

	c.HTML(200, "template/search.html", map[string]interface{}{
		"Blogs":      blogs,
		"Total":      total,
		"Page":       page,
		"Categories": allCategory,
		"Tags": allTags,
		"Keywords": keywords,
		"Method": method,
	})

}

// GetDetailPage 获取文章详细页
func GetDetailPage(c *gin.Context) {
	bid, err := strconv.Atoi(c.Param("bid"))
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "")
	}

	blogContent, err := new(dao.BlogDao).GetBlogDetailByBid(bid)
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "blog_content")
	}

	blog, err := new(dao.BlogDao).GetBlogById(bid)
	if err != nil {
		Response.Err(c, 200, 500, err.Error(), "")
	}

	c.HTML(http.StatusOK, "template/post.html", gin.H{
		"Title": "文章详情页",
		"BlogContent": blogContent.BlogContent,
		"Blog":        blog,
	})

}

// GetContactPage 获取联系我的页面
func GetContactPage(c *gin.Context) {
	c.HTML(200, "template/contact.html", map[string]interface{}{
		"Title": "联系我",
	})
}

func GetArchivePage(c *gin.Context) {
	calendar, err := new(dao.BlogDao).PartitionTable()
	if err != nil {
		return
	}
	log.Println(calendar)
	c.HTML(200, "template/archive.html", map[string]interface{}{
		"Title": "归档",
		"Calendar": calendar,

	})
}

// AddMessage 添加消息
func AddMessage(c *gin.Context) {
	var message = &models.Message{}
	var json map[string]interface{}
	err := c.ShouldBind(&json)
	if err != nil {
		log.Println(err)
	}
	submissionObject := json["submissionObject"]
	message.Name = submissionObject.(map[string]interface{})["name"].(string)
	message.Email = submissionObject.(map[string]interface{})["email"].(string)
	message.Phone = submissionObject.(map[string]interface{})["phone"].(string)
	message.Message = submissionObject.(map[string]interface{})["message"].(string)

	err = new(dao.MessageDao).AddMessage(message)
	if err != nil {
		log.Println(err)
		return
	}

	// 添加成功
	return
}
